package com.eastfair.home.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.echo.core.EchoService;
import com.eastfair.home.dto.ReportDrawingAuditHistoryPageQuery;
import com.eastfair.home.dto.ReportDrawingAuditHistorySaveDTO;
import com.eastfair.home.dto.ReportDrawingAuditHistoryUpdateDTO;
import com.eastfair.home.entity.ReportDrawingAuditHistory;
import com.eastfair.home.service.ReportDrawingAuditHistoryService;
import com.eastfair.home.vo.ReportDrawingAuditHistoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 报馆图纸审核记录历史
 * </p>
 *
 * @author dq
 * @date 2022-08-08
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/reportDrawingAuditHistory")
@Api(value = "ReportDrawingAuditHistory", tags = "报馆图纸审核记录历史")
@PreAuth(replace = "home:reportDrawingAuditHistory:")
public class ReportDrawingAuditHistoryController extends SuperController<ReportDrawingAuditHistoryService, Long, ReportDrawingAuditHistory, ReportDrawingAuditHistoryPageQuery, ReportDrawingAuditHistorySaveDTO, ReportDrawingAuditHistoryUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private ReportDrawingAuditHistoryService reportDrawingAuditHistoryService;

    @Override
    public void handlerResult(IPage<ReportDrawingAuditHistory> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<ReportDrawingAuditHistory> reportDrawingAuditHistoryList = list
                .stream()
                .map((map) -> ReportDrawingAuditHistory.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(reportDrawingAuditHistoryList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ReportDrawingAuditHistoryPageQuery> params) {
        ReportDrawingAuditHistoryPageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 图纸分类审核历史ID查询图纸审核历史列表.
     *
     * @param reportDrawingCategoryAuditHistoryId the report drawing category audit history id
     * @return the r
     */
    @ApiOperation("图纸分类审核历史ID查询图纸审核历史列表")
    @GetMapping("/reportDrawingCategoryAuditHistory/{reportDrawingCategoryAuditHistoryId}")
    public R<List<ReportDrawingAuditHistoryVO>> queryByReportDrawingCategoryAuditHistoryId(@PathVariable("reportDrawingCategoryAuditHistoryId")
                                                                                           Long reportDrawingCategoryAuditHistoryId) {
        log.info("queryByReportDrawingCategoryAuditHistoryId - 图纸分类审核历史ID查询图纸审核历史列表, reportDrawingCategoryAuditHistoryId={}", reportDrawingCategoryAuditHistoryId);
        List<ReportDrawingAuditHistoryVO> list = reportDrawingAuditHistoryService.listByReportDrawingCategoryAuditHistoryId(reportDrawingCategoryAuditHistoryId);
        return R.successDef(list);
    }

}
